using JwtAuthDemo.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using webapi.Domain.Repository;
using Webapi.Domain.Entity;

namespace webapi.api.Controllers
{
    //授权过滤器
    [Authorize]
    [Route("[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly IRepository<Users> _users;
        private readonly JwtHelpers _jwtHelpers;
        public LoginController(JwtHelpers jwtHelpers,IRepository<Users> users)
        {
            _jwtHelpers = jwtHelpers;
            _users = users;
        }
        //任何请求都能访问该方法
        [AllowAnonymous]
        [HttpPost, Route("signin")]
        public IActionResult Login(LoginModel model)
        {
            System.Console.WriteLine("---------------------------------");
            if (ValidateUser(model))
            {
                var token = _jwtHelpers.GenerateToken(model.UserName);
                var list = new List<dynamic>{
                    new {username=model.UserName},
                    new {password=model.Password},
                    new {token=token},
                };
                return Ok(list);
            }
            else
            {
                return BadRequest();
            }
        }
        [HttpGet, Route("ValidateUser")]
        public bool ValidateUser(LoginModel model)
        {
            return true;
        }
        [HttpGet, Route("Test")]
        public IActionResult Test()
        {
            return Ok("Test");
        }


        public class LoginModel
        {
            public string UserName { get; set; }

            public string Password { get; set; }
        }
    }
}